import logging

import pytest

from resource.pom.settings import Settings


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击设置应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.进入设置应用')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击声音')
        device.click(*self.app.sounds_item)
        logging.info('预期结果2.进入声音')
        device.refresh_layout()
        device.assert_text_exist('声音')
        device.assert_text_exist('声音模式')
        device.assert_type_exist('Radio')
        device.assert_text_exist('音量')
        device.assert_text_exist('来电')
        device.assert_text_exist('媒体')
        device.assert_text_exist('通话')
        device.assert_type_exist('Slider')

        logging.info('步骤3.左右滑动或点击“来电、媒体、通话”滚动条')
        ring = device.get_element_by_type('Slider')
        rlx, rly, rrx, rry = device.get_element_area(ring)
        media = device.get_element_by_type('Slider', index=1)
        mlx, mly, mrx, mry = device.get_element_area(media)
        call = device.get_element_by_type('Slider', index=2)
        clx, cly, crx, cry = device.get_element_area(call)
        # 从中间划到最左边滑
        device.swipe((rlx + rrx) // 2, (rly + rry) // 2, rlx + 50, (rly + rry) // 2)
        device.swipe((mlx + mrx) // 2, (mly + mry) // 2, rlx + 50, (mly + mry) // 2)
        device.swipe((clx + crx) // 2, (cly + cry) // 2, rlx + 50, (cly + cry) // 2)
        device.wait(3)
        device.refresh_layout()
        r_volume0 = float(device.get_element_by_type('Slider').get('text'))
        m_volume0 = float(device.get_element_by_type('Slider', index=1).get('text'))
        c_volume0 = float(device.get_element_by_type('Slider', index=2).get('text'))
        volume0 = device.get_volume()

        # 从最左边划到最右边
        device.swipe(rlx, (rly + rry) // 2, rrx - 50, (rly + rry) // 2)
        device.swipe(mlx, (mly + mry) // 2, mrx - 50, (mly + mry) // 2)
        device.swipe(clx, (cly + cry) // 2, crx - 50, (cly + cry) // 2)
        device.wait(3)
        device.refresh_layout()
        r_volume1 = float(device.get_element_by_type('Slider').get('text'))
        m_volume1 = float(device.get_element_by_type('Slider', index=1).get('text'))
        c_volume1 = float(device.get_element_by_type('Slider', index=2).get('text'))
        volume1 = device.get_volume()
        logging.info('预期结果3.无卡顿现象，实际音量随之变化')
        assert r_volume0 < r_volume1, '来电音量进度条变化{}->{}'.format(r_volume0, r_volume1)
        assert m_volume0 < m_volume1, '媒体音量进度条变化{}->{}'.format(m_volume0, m_volume1)
        assert c_volume0 < c_volume1, '通话音量进度条变化{}->{}'.format(c_volume0, c_volume1)

        assert volume0['ring'] < volume1['ring'], '来电音量变化{}->{}'.format(r_volume0, r_volume1)
        assert volume0['music'] < volume1['music'], '媒体音量变化{}->{}'.format(r_volume0, r_volume1)
        assert volume0['camcorder'] < volume1['camcorder'], '媒体音量变化{}->{}'.format(r_volume0, r_volume1)
        assert volume0['wakeup'] < volume1['wakeup'], '媒体音量变化{}->{}'.format(r_volume0, r_volume1)
        assert volume0['call'] < volume1['call'], '通话音量变化{}->{}'.format(r_volume0, r_volume1)
        assert volume0['communication'] < volume1['communication'], '通话音量变化{}->{}'.format(r_volume0, r_volume1)
